package com.example.apple.hxbtc.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.apple.hxbtc.R;
import com.example.apple.hxbtc.activity.BtcWebActivity;
import com.example.apple.hxbtc.activity.RefreshActivity;
import com.example.apple.hxbtc.activity.UserLoginActivity;
import com.example.apple.hxbtc.application.App;
import com.example.apple.hxbtc.loader.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hpz on 17/2/21.
 */

public class HomeFragment extends Fragment implements AdapterView.OnItemClickListener, OnBannerListener {
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.main_btn)
    ImageButton mainBtn;
    @BindView(R.id.banner)
    Banner banner;
    //@BindView(R.id.swipe)
    //SwipeRefreshLayout swipe;

    static final int REFRESH_COMPLETE = 0X1112;
    @BindView(R.id.main_btc_login)
    TextView mainBtcLogin;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    String[] urls = getResources().getStringArray(R.array.url4);
                    List list = Arrays.asList(urls);
                    List arrayList = new ArrayList(list);
                    banner.update(arrayList);
                    mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
                    //swipe.setRefreshing(false);
                    break;
            }
        }
    };


        /*webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.hxnidc.com");

        img_btn = (ImageButton) getActivity().findViewById(R.id.main_btn);
        img_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(img_btn);
            }

            private void showPopupMenu(View view) {
                // View当前PopupMenu显示的相对View的位置
                *//*PopupMenu popupMenu = new PopupMenu(getActivity(), view);
                // menu布局
                popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
                // menu的item点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
                // PopupMenu关闭事件
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {
                    }
                });*//*
                PopupMenu popup = new PopupMenu(getActivity(), view);

                popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.action_one:
                                Intent one = new Intent();
                                one.setClass(getActivity(), BtcChinaActivity.class);
                                startActivity(one);
                                return true;
                            case R.id.action_two:
                                Intent two = new Intent();
                                two.setClass(getActivity(), OkCoinActivity.class);
                                startActivity(two);
                                return true;
                            case R.id.action_three:
                                Intent three = new Intent();
                                three.setClass(getActivity(), HuobiActivity.class);
                                startActivity(three);
                                return true;
                        }
                        return false;
                    }
                });
                popup.show();
            }

        });*/


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {

        //简单使用
        banner.setImages(App.images)
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(this)
                .start();
        banner.startAutoPlay();

    }

    private void initView() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.hxnidc.com");
/*
        mainBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(mainBtn);
            }

            private void showPopupMenu(View view) {
                // View当前PopupMenu显示的相对View的位置
                */
/*PopupMenu popupMenu = new PopupMenu(getActivity(), view);
                // menu布局
                popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
                // menu的item点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
                // PopupMenu关闭事件
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {
                    }
                });*//*

                PopupMenu popup = new PopupMenu(getActivity(), view);

                popup.getMenuInflater().inflate(R.menu.main_btc, popup.getMenu());

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.action_one:
                                Intent one = new Intent();
                                one.setClass(getActivity(), BtcChinaActivity.class);
                                startActivity(one);
                                return true;
                            case R.id.action_two:
                                Intent two = new Intent();
                                two.setClass(getActivity(), OkCoinActivity.class);
                                startActivity(two);
                                return true;
                            case R.id.action_three:
                                Intent three = new Intent();
                                three.setClass(getActivity(), HuobiActivity.class);
                                startActivity(three);
                                return true;
                        }
                        return false;
                    }
                });
                popup.show();
            }

        });
*/
        mainBtcLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent btn_main = new Intent();
                btn_main.setClass(getActivity(), UserLoginActivity.class);
                startActivity(btn_main);
            }
        });

        mainBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent btn_button = new Intent();
                btn_button.setClass(getActivity(), RefreshActivity.class);
                startActivity(btn_button);
            }
        });


    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void OnBannerClick(int position) {
        //Toast.makeText(getActivity(), "你点击了：" + position, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setClass(getActivity(),BtcWebActivity.class);
        startActivity(intent);


    }

    @Override
    public void onDestroy() {
        banner.stopAutoPlay();
        super.onDestroy();
    }
}
